function Renderer(map, cIcon, name) {
	this.icon = cIcon.getIcon();
	this.map = map;
	this.marker = new GMarker(this.map.getCenter(),{draggable:true,icon:this.icon});
	this.name = name;
	this.imageUrl;
	this.handles = {};
	this.map.addOverlay(this.marker);
	GEvent.trigger(this.map, "newmapobject");
	GEvent.bind(this.map, "newmapobject", this, this.deactivate);
	GEvent.bind(this.map, "clearselection", this, this.deactivate);
	GEvent.bind(this.map, "mapobjectactivate", this, this.deactivate);
};
Renderer.prototype.getIconUrl = function() {
	return this.marker.getIcon().image;
};
Renderer.prototype.toString = function() {
	return this.name;
};
Renderer.prototype.setIcon = function(disableIcon) {
	var img;
	if (disableIcon) {
		img = 'js/map/annotate/icons/colour063.png';
	} else {
		img = this.getIconUrl();
	}

	if (this.imageUrl!=img) {
		this.marker.setImage(img);
		this.imageUrl = img;
	}
};

Renderer.prototype.activate = function() {
	GEvent.trigger(this.map, "mapobjectactivate");
	GEvent.addListener(this.map,'click',this.getMapClickHandler(this));
	GEvent.bind(this.marker, "dragstart", this, this.dragStart);
	this.handles.removepoint = GEvent.addListener(this.map,"removepoint",this.getRemovePointHandler(this));
	this.handles.click = GEvent.addListener(this.marker,"click",this.getClickHandler(this));
	this.handles.removeallpoints = GEvent.addListener(this.map,"removeallpoints",this.getRemoveAllHandler(this));
	this.marker.enableDragging();
	this.setIcon();	
	this.click();
	this.active = true;
};
Renderer.prototype.dragStart = function() {
	this.map.getInfoWindow().hide();
};
Renderer.prototype.getRemovePointHandler = function(instance) {
	return function(){
		instance.removeLastPoint();
	};
};
Renderer.prototype.getClickHandler = function(instance) {
	return function(){
		instance.click();
	};
};
Renderer.prototype.click = function() {
	this.marker.openInfoWindowHtml("<p>" +this.toString()+ "</p>");
};
Renderer.prototype.getRemoveAllHandler = function(instance) {
	return function(){
		instance.removeAllPoints();
	};
};
Renderer.prototype.deactivate = function() {
	if (this.active) {
		GEvent.clearInstanceListeners(this.marker);
		GEvent.clearListeners(this.map,'click');
		this.map.getInfoWindow().hide();
		this.marker.disableDragging();
		try {GEvent.removeListener(this.handles.removepoint);}catch(e){}
		try {GEvent.removeListener(this.handles.click);}catch(e){}
		try {GEvent.removeListener(this.handles.removeallpoints);}catch(e){}
		GEvent.addListener(this.marker,"click",this.getActivateHandler(this));
		this.setIcon(true);	
		this.active = false;
	}
};
Renderer.prototype.getActivateHandler = function(instance) {
	return function(){
		if (!instance.active) {
			instance.activate();
		}
	};
};
Renderer.prototype.getMapClickHandler = function(instance) {
	return function(overlay,  point){
		instance.map.getInfoWindow().hide();
		if (instance.active) {
			if (point) {
				instance.setMarker(point);
			}
		}
	};
};
/* Start the color object  ************************************************************* */

function CIcon(iconNumber,lineColor,color) {
	this.iconNumber = iconNumber;
	this.lineColor = lineColor;
	this.color = color;
}	
CIcon.prototype.getIcon = function(){

	// Create an actual marker
	var icon = new GIcon(G_DEFAULT_ICON,this.getUrl());

	return icon;
};
CIcon.prototype.getUrl = function(){
	return this.path + this.iconNumber + this.extension;
};
CIcon.prototype.path = 'js/map/annotate/icons/colour';
CIcon.prototype.extension = '.png';

/* start the poly menu Object ************************************************************* */
function PolyControls() {
}
PolyControls.prototype = new GControl();

PolyControls.prototype.initialize = function(map) {

  var container = document.createElement("div");

	var addPointDiv = document.createElement("div");
	this.setButtonStyle_(addPointDiv);
	container.appendChild(addPointDiv);
	addPointDiv.appendChild(document.createTextNode("Add a Point"));
	GEvent.addDomListener(addPointDiv, "click", function() {
		GEvent.trigger(map, "addPoint");
	});

	var addPolyDiv = document.createElement("div");
	this.setButtonStyle_(addPolyDiv);
	container.appendChild(addPolyDiv);
	addPolyDiv.appendChild(document.createTextNode("Add a Polygon"));
	GEvent.addDomListener(addPolyDiv, "click", function() {
		GEvent.trigger(map, "addPoly");
	});
	
	var addPolylineDiv = document.createElement("div");
	this.setButtonStyle_(addPolylineDiv);
	container.appendChild(addPolylineDiv);
	addPolylineDiv.appendChild(document.createTextNode("Add a Polyline"));
	GEvent.addDomListener(addPolylineDiv, "click", function() {
		GEvent.trigger(map, "addPolyline");
	});
	
	var clearDiv = document.createElement("div");
	this.setButtonStyle_(clearDiv);
	container.appendChild(clearDiv);
	clearDiv.appendChild(document.createTextNode("Clear Selection"));
	GEvent.addDomListener(clearDiv, "click", function() {
		GEvent.trigger(map, "clearselection");
	});
	
	var sepDiv = document.createElement("div");
	this.setButtonStyle_(sepDiv);
	container.appendChild(sepDiv);
	sepDiv.appendChild(document.createTextNode(" "));
	sepDiv.style.backgroundColor = "black";

	var closeLinkDiv = document.createElement("div");
	this.setButtonStyle_(closeLinkDiv);
	container.appendChild(closeLinkDiv);
	closeLinkDiv.appendChild(document.createTextNode("Close Shape"));
	GEvent.addDomListener(closeLinkDiv, "click", function() {
		GEvent.trigger(map, "closeshape");
	});

	var removeLinkDiv = document.createElement("div");
	this.setButtonStyle_(removeLinkDiv);
	container.appendChild(removeLinkDiv);
	removeLinkDiv.appendChild(document.createTextNode("Remove a Point"));
	GEvent.addDomListener(removeLinkDiv, "click", function() {
		GEvent.trigger(map, "removepoint");
	});
	
	var removePolyLinkDiv = document.createElement("div");
	this.setButtonStyle_(removePolyLinkDiv);
	container.appendChild(removePolyLinkDiv);
	removePolyLinkDiv.appendChild(document.createTextNode("Remove Selection"));
	GEvent.addDomListener(removePolyLinkDiv, "click", function() {
		GEvent.trigger(map, "removeallpoints");
	});
	

	
  map.getContainer().appendChild(container);
  return container;
};

PolyControls.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7, 50));
};

PolyControls.prototype.setButtonStyle_ = function(button) {
  button.style.textDecoration = "none";
  /*button.style.color = "#0000cc";*/
  button.style.backgroundColor = "white";
  button.style.font = "small Arial";
  button.style.border = "1px solid black";
  button.style.padding = "2px";
  button.style.marginBottom = "3px";
  button.style.textAlign = "center";
  button.style.width = "7em";
  button.style.cursor = "pointer";
};
/* end poly controls*/
